﻿#include "CRobFactHy.h"
#include "Robot.h"
#include "CDevHy.h"
#include "CSqlOper.h"
CRobFactHy::CRobFactHy(QString sSceneGuid)
{
	m_sGuid = sSceneGuid;

}

CRobFactHy::~CRobFactHy()
{
}

void CRobFactHy::createAll(void * pData)
{
	Robot *pRob = (Robot *)pData;
	/**
	* @ Author: Gaojia
	* @ Description: 创建设备对象
	*/
	m_pRobDev= createDev();
	m_pDevData = m_pRobDev->m_pDevDataBase;
	/**
	* @ Author: Gaojia
	* @ Description: 创建基础数据
	*/
	m_pRBaseData = createBasicData();
	CLogOper::instance().addDevName(m_pRobDev->m_pDevDataBase->m_sRobName, m_sGuid);
	/**
	* @ Author: Gaojia
	* @ Description: 创建业务逻辑对象
	*/
	m_pWorkMngrBase = createWorkMngr();

	/**
	* @ Author: Gaojia
	* @ Description: 创建主界面
	*/
	URobMainBase *pRobMainUi = createMainWindow();
	/**
	* @ Author: Gaojia
	* @ Description: 给pData赋值
	*/
	pRob->m_pRobDev = m_pRobDev;
	pRob->m_pDevData = m_pDevData;
	pRob->m_pBaseData = m_pRBaseData;
	pRob->m_pWorkMngrBase = m_pWorkMngrBase;
	pRob->m_pRobMainUi = pRobMainUi;

	/**
	* @ Author: Gaojia
	* @ Description: 创建测点管理对象
	*/
	CPointMgrBase *pPointMgrBase = createMngrBase();
	m_pPointMgrBase = pPointMgrBase;
	pRob->m_pPointMgrBase = pPointMgrBase;
}


//创建新设备
void CRobFactHy::addAll(CDevDataBase* pDevData, void* pData)
{
	m_pDevData = pDevData;
	Robot* pRob = (Robot*)pData;
	m_sName = pRob->m_sName;
	m_sId = pRob->m_sId;

	//添加默认数据 并存到数据库
	addDevice();

	CLogOper::instance().addDevName(m_sName, m_sGuid);
	/**
	* @ Author: Gaojia
	* @ Description: 添加设备对象
	*/
	m_pRobDev = addDev();
	m_pDevData = m_pRobDev->m_pDevDataBase;
	/**
	* @ Author: Gaojia
	* @ Description: 添加基础数据
	*/
	m_pRBaseData = addBasicData();
	/**
	* @ Author: Gaojia
	* @ Description: 添加业务逻辑对象
	*/
	m_pWorkMngrBase = addWorkMngr();

	/**
	* @ Author: Gaojia
	* @ Description: 添加主界面
	*/
	URobMainBase* pRobMainUi = createMainWindow();
	/**
	* @ Author: Gaojia
	* @ Description: 给pData赋值
	*/
	pRob->m_pRobDev = m_pRobDev;
	pRob->m_pDevData = m_pDevData;
	pRob->m_pBaseData = m_pRBaseData;
	pRob->m_pWorkMngrBase = m_pWorkMngrBase;
	pRob->m_pRobMainUi = pRobMainUi;

	/**
	* @ Author: Gaojia
	* @ Description: 创建测点管理对象
	*/
	CPointMgrBase* pPointMgrBase = createMngrBase();
	m_pPointMgrBase = pPointMgrBase;
	pRob->m_pPointMgrBase = pPointMgrBase;
}


CDevBase * CRobFactHy::createDev()
{
	CDevBase *pRobDev = new CDevHy(m_sGuid);
	return pRobDev;
}

CDevBase* CRobFactHy::addDev()
{
	CDevBase* pRobDev = new CDevHy(m_pDevData);
	return pRobDev;
}

CRBaseData * CRobFactHy::createBasicData()
{
	CRBaseData * pRBaseData = new CRBaseData();
	pRBaseData->m_pPolArea = new CPolArea();
	QString sErrMsg = "";
	selAreaByGuid(m_sGuid, pRBaseData->m_pPolArea, sErrMsg);
	selPointByGuiD(m_sGuid, pRBaseData->m_pPolArea->m_mapPolPoint, sErrMsg);
	selItemCamerByGuid(m_sGuid, pRBaseData->m_pPolArea->m_mapPolPoint, sErrMsg);
	selItemSensorByGuid(m_sGuid, pRBaseData->m_pPolArea->m_mapPolPoint, sErrMsg);
	m_pRBaseData = pRBaseData;
	return pRBaseData;
}

CWorkMngrBase * CRobFactHy::createWorkMngr()
{
	CWorkMngrBase *pWorkMngrBase = new CWorkMngAutoHy();
	pWorkMngrBase->init(m_sGuid, m_pRobDev, m_pRBaseData);
	pWorkMngrBase->startWork();
	return pWorkMngrBase;
}

URobMainBase * CRobFactHy::createMainWindow()
{
	URobMainBase* pRobMain = new URobotMainUi();
	pRobMain->initUI(m_sGuid, m_pRobDev, m_pRBaseData);
	return pRobMain;
}

CPointMgrBase * CRobFactHy::createMngrBase()
{
	CPointMgrBase *pPointMgrBase = new CPointMgrBase(m_pDevData, m_pRBaseData);
	m_pDevData->m_pPointMgrBase = pPointMgrBase;
	return pPointMgrBase;
}

/**
* @ Author: Gaojia
* @ Description: 获取指定面板
*/
QWidget * CRobFactHy::getPanel(int nId)
{
	return createPanel(nId);
}

/**
* @ Author: Gaojia
* @ Description:  获取面板列表
*/
QVector<QWidget*> CRobFactHy::getPanelList()
{
	if (m_vecWidget.size() > 0)
	{
		return m_vecWidget;
	}
	for (int i = 0; i < enPanelCount; i++)
	{
		QWidget *pPanel = createPanel(i);
		registerUi(pPanel, i);
	}
	return m_vecWidget;
}

CRBaseData* CRobFactHy::addBasicData()
{
	CRBaseData* pRBaseData = new CRBaseData();
	pRBaseData->m_pPolArea = new CPolArea();
	m_pRBaseData = pRBaseData;
	return pRBaseData;
}

CWorkMngrBase* CRobFactHy::addWorkMngr()
{
	CWorkMngrBase* pWorkMngrBase = new CWorkMngAutoHy();
	pWorkMngrBase->init(m_sGuid, m_pRobDev, m_pRBaseData);
	pWorkMngrBase->startWork();
	return pWorkMngrBase;
}

URobMainBase* CRobFactHy::addMainWindow()
{
	URobMainBase* pRobMain = new URobotMainUi();
	pRobMain->initUI(m_sGuid, m_pRobDev, m_pRBaseData);
	return pRobMain;
}

CPointMgrBase* CRobFactHy::addMngrBase()
{
	CPointMgrBase* pPointMgrBase = new CPointMgrBase(m_pDevData, m_pRBaseData);
	return pPointMgrBase;
}


/**
* @ Author: Gaojia
* @ Description:  创建指定id的模板
*/
QWidget * CRobFactHy::createPanel(int nId)
{
	UBasePanel *pPanel = nullptr;
	switch (nId)
	{
	case enCtrPanel:
		pPanel = new UCtrPanel();
		pPanel->setProperty("row", 1);
		pPanel->setProperty("col", 1);
		pPanel->setProperty("name", A2T("运动控制"));
		pPanel->initPanel(m_sGuid, m_pRobDev, m_pRBaseData);
		
		break;
	case enUModelPanel:
		pPanel = new UModePanel();
		pPanel->setProperty("row", 1);
		pPanel->setProperty("col", 1);
		pPanel->setProperty("name", A2T("模式控制"));
		pPanel->initPanel(m_sGuid, m_pRobDev, m_pRBaseData);
		break;
	case enVideoPanel:
		pPanel = new UVideoPanel();
		pPanel->setProperty("row", 2);
		pPanel->setProperty("col", 3);
		pPanel->setProperty("name", A2T("视频显示"));
		pPanel->initPanel(m_sGuid, m_pRobDev, m_pRBaseData);
		break;
	case enStateInfoPanel:
		pPanel = new UStateInfoPanel();
		pPanel->setProperty("row", 1);
		pPanel->setProperty("col", 1);
		pPanel->setProperty("name", A2T("状态信息"));
		pPanel->initPanel(m_sGuid, m_pRobDev, m_pRBaseData);
		break;
	case enRobDataTabWidget:
		pPanel = new CRobDataTabWidget();
		pPanel->setProperty("row", 2);
		pPanel->setProperty("col", 2);
		pPanel->setProperty("name", A2T("数据显示"));
		pPanel->initPanel(m_sGuid, m_pRobDev, m_pRBaseData);
		break;
	case enMainInfoPanel:
		pPanel = new UMainInfoPanel();
		pPanel->setProperty("row", 1);
		pPanel->setProperty("col", 1);
		pPanel->setProperty("name", A2T("关键数据"));
		pPanel->initPanel(m_sGuid, m_pRobDev, m_pRBaseData);
		break;
	case enUMapPanel:
		pPanel = new UMapPanel();
		pPanel->setProperty("row", 1);
		pPanel->setProperty("col", 2);
		pPanel->setProperty("name", A2T("地图显示"));
		pPanel->initPanel(m_sGuid, m_pRobDev, m_pRBaseData);
		break;
	default:
		break;
	}
	return pPanel;
}


void CRobFactHy::registerUi(QWidget * pWidget, int nId)
{
	pWidget->setProperty("id", nId);
	m_vecWidget.push_back(pWidget);
}



void CRobFactHy::addDevice()
{
	QString sErrMsg = "";
	//插入prob_device_info表
	intsertDeviceInfo(m_pDevData, sErrMsg);
	m_pDevData->m_dRobRunSpeed = 100;
	m_pDevData->m_dRobRunSpeedZ = 100;
	m_pDevData->m_nDeviceEnum = 1001;
	m_pDevData->m_nRobType = 1;
	m_pDevData->m_dSafeDistance = 0;
	m_pDevData->m_nRobWorkType = 1;
	m_pDevData->m_dCorse = 0;
	m_pDevData->m_nProximitySensorOffset = 0;
	m_pDevData->m_nNo2LabelOffset = 0;
	m_pDevData->m_nIsOpenAutoCharge = 0;
	m_pDevData->m_nAutoChargeThreshold = 20;
	m_pDevData->m_dReturnSpeed = 100;
	m_pDevData->m_dFrontObstacleAvoidance = 0;
	m_pDevData->m_dBackObstanceAvidance = 0;
	m_pDevData->m_dChargeCutOffThreshold = 90;
	m_pDevData->m_dChargeCutOffVolTage = 28.5;
	m_pDevData->m_dHoldingAddPowerThreshold = 70;
	m_pDevData->m_dHoldingAddPowerVoltage = 27.3;
	m_pDevData->m_dTaskEleQuantityThreshold = 50;
	m_pDevData->m_dTaskEleQuantityVoltage = 24.9;
	m_pDevData->m_dForceChargeThreshold = 30;
	m_pDevData->m_dForceChargeVoltage = 24.5;
	m_pDevData->m_dChargeFallLoopCount = 20;
	m_pDevData->m_dChargeCommunicationDelay = 33;
	m_pDevData->m_dChargeAutoRetornDistance = 6;
	m_pDevData->m_dLevelOneSpeedCut = 0.3;
	m_pDevData->m_dLevelTwoSpeedCut = 0.02;
	m_pDevData->m_dDriveMotorUpperCurrent = 1.4;
	m_pDevData->m_dDriveMotorCurrentSleep = 200;
	m_pDevData->m_dDriveMotorUpperTorque = 1.4;
	m_pDevData->m_dDriveMotorTorqueSleep = 200;
	m_pDevData->m_dDriveMotorUpperTemp = 60;
	m_pDevData->m_dDriveMotorTempSleep = 1000;
	m_pDevData->m_dLiftMotorUpperCurrent = 1.1;
	m_pDevData->m_dLiftMotorCurrentSleep = 100;
	m_pDevData->m_dLiftMotorUpperTorque = 1.1;
	m_pDevData->m_dLiftMotorTorqueSleep = 100;
	m_pDevData->m_dLiftMotorUpperTemp = 60;
	m_pDevData->m_dLiftMotorTempSleep = 1000;
	m_pDevData->m_dLiftMotorSleepSet = 100;
	m_pDevData->m_dOneClickBaceLocation = 0;
	m_pDevData->m_dAutoSwitchOpenBatteryThre = 20;		    //自动分闸电量阈值(%)
	m_pDevData->m_dAutoSwitchOpenVolThre = 23.8;			    //自动分闸电压阈值(V)
	m_pDevData->m_dIsAutoFlush = 0;						    //巡检结束是否冲洗
	m_pDevData->m_nFlushRFID1 = 0;						    //冲洗位置前RFID号
	m_pDevData->m_nFlushRFID2 = 0;                            //冲洗位置后RFID号
	m_pDevData->m_dFlushPlaceOffset = 0;				        //冲洗位置补偿
	m_pDevData->m_nChargeYTPreset = 0;
	m_pDevData->m_nAlarmStopTime = 60;
	m_pDevData->m_nCollectTime = 3;
	m_pDevData->m_nVoiceAverigeTime = 3;
	bool bRet = addRobHost(m_sGuid, m_pDevData, sErrMsg);
	if (false == bRet)
	{
		messageBox(2, A2T("数据库执行失败!失败原因: ") + sErrMsg);
		return;
	}

	/*插入modbus地址数据*/
	QList<QStringList> listModbusData;
	addDefRobModbusData(listModbusData);
	bRet = addRobModbus(m_sGuid, listModbusData, sErrMsg);
	if (false == bRet)
	{
		messageBox(2, A2T("数据库执行失败!失败原因: ") + sErrMsg);
		return;
	}

	/*绑定默认传感器*/
	QVector<SSensorInfo> vecSensorInfo;
	addSensora(vecSensorInfo);
	bRet = addRobSensorMulti(m_sGuid, vecSensorInfo, sErrMsg);
	if (false == bRet)
	{
		messageBox(2, A2T("数据库执行失败!失败原因: ") + sErrMsg);
		return;
	}
	bRet = addRobPanTilt(m_pDevData->m_pDevDataCam, sErrMsg);
	if (false == bRet)
	{
		messageBox(2, A2T("数据库执行失败!失败原因: ") + sErrMsg);
		return;
	}
}

void CRobFactHy::addSensora(QVector<SSensorInfo>& vecSensorData)
{
	SSensorInfo stSensorData;
	stSensorData.m_bEnaled = true;
	stSensorData.m_sGuid = createGuid();
	stSensorData.m_sSnesorType = "0";
	stSensorData.m_sSensorName = A2T("一氧化碳传感器");
	vecSensorData.append(stSensorData);
	stSensorData.m_bEnaled = true;
	stSensorData.m_sGuid = createGuid();
	stSensorData.m_sSnesorType = "1";
	stSensorData.m_sSensorName = A2T("可燃气体传感器");
	vecSensorData.append(stSensorData);
	stSensorData.m_bEnaled = true;
	stSensorData.m_sGuid = createGuid();
	stSensorData.m_sSnesorType = "2";
	stSensorData.m_sSensorName = A2T("PM2.5传感器");
	vecSensorData.append(stSensorData);
	stSensorData.m_bEnaled = true;
	stSensorData.m_sGuid = createGuid();
	stSensorData.m_sSnesorType = "3";
	stSensorData.m_sSensorName = A2T("超声波传感器");
	vecSensorData.append(stSensorData);
	stSensorData.m_bEnaled = true;
	stSensorData.m_sGuid = createGuid();
	stSensorData.m_sSnesorType = "4";
	stSensorData.m_sSensorName = A2T("温度传感器");
	vecSensorData.append(stSensorData);
	stSensorData.m_bEnaled = true;
	stSensorData.m_sGuid = createGuid();
	stSensorData.m_sSnesorType = "5";
	stSensorData.m_sSensorName = A2T("湿度传感器");
	vecSensorData.append(stSensorData);
	stSensorData.m_bEnaled = true;
	stSensorData.m_sGuid = createGuid();
	stSensorData.m_sSnesorType = "6";
	stSensorData.m_sSensorName = A2T("PM10传感器");
	vecSensorData.append(stSensorData);
}

void CRobFactHy::addDefRobModbusData(QList<QStringList>& listModbusData)
{
	QString sPath = QCoreApplication::applicationDirPath();
	sPath.append("/DLL/RobCfg/defaultModbusCfg.csv");

	QFile file(sPath);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		messageBox(2, A2T("读取默认Modbus数据失败!"));
		return;
	}

	QTextStream stream(&file);
	int nIndex = 0;
	while (!stream.atEnd())
	{
		QString line = stream.readLine();
		QStringList row = line.split(","); // 使用逗号分隔数据
		listModbusData.append(row);
	}

	file.close();

	//移除第1个标题
	listModbusData.removeAt(0);
}